/**********************************************************************/
/*
   Author: Michelle Han
   Created: Mar 2023
   Updated: June 2024
   Description: Cleans raw qualtrics survey data from 2021 survey. To be run on
   qualtrics dataset exported as excel file with choice text, with seen but unanswered
   questions coded as -99.

   This file is based on the cleaning for 2022 survey, to generate comparable baseline variables.

   Note that the output for this file contains only completed, verified and consented responses

   This cleaning file should output 2 different datasets:
   1. Cleaned Survey 21 data:
   071323_survey_21_deid_clean.dta
   2. Survey 21 person-batch data matched with PMO:
   071323_survey_21_deid_clean_merged
*/
/**********************************************************************/

/*----------------------------------------------------*/
 * Section: Set-up and import de-identified data
/*----------------------------------------------------*/

* include filepaths
  	if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"

	clear
	set more off

	* Log
	cap log close
	global prefix: display %tdCYND td(`c(current_date)')
	log using "$KP_logs/${prefix}_clean_survey_21.txt", text replace

	
	* Load data
	use "$KP_deid_survey/2021/Raw/survey_raw_final_all_deid.dta", clear

	* Set trim values
	gen inc_sak_u = 10000000 // 99% percentile in sak aug '21
	gen inc_sak_l = 200000 // 5% percentile in sak aug '21
	gen wage_sak_u = 65000
	gen wage_sak_l = 0
	gen hrs_sak_u = 168
	gen food_sus_u = 8039887
	gen food_sus_l = 574206.6
	gen nfood_sus_u = 12143393
	gen nfood_sus_l = 348552.8

/*----------------------------------------------------*/
 * Section: Initial format
/*----------------------------------------------------*/

*********************************
* Destring numbers

	local destring_list progress ///
						durationinseconds ///
						kk03 ///
						kk04 ///
						dk07_2 ///
						dk09_1_* ///
						dk14_1_* ///
						dk07_v2_2 ///
						dk09_v2_1_* ///
						dk14_v2_1_* ///
						dk23_2 ///
						tr09_1 ///
						tr10 ///
						kf10_2 ///
						mobile ///
						batch ///
						work_hour_1 ///
						work_hour_2 ///
						re_random ///
						gender ///
						year_dob_inputted_plus2 ///
						year_dob_inputted_minus2 ///
						year_dob ///
						dk06_dk10 ///
						ps01_ps02_last

	foreach i of varlist `destring_list' {
		destring `i', replace
		replace `i' = .a if `i' == -99
	}



*********************************
* Format date variables

	foreach timevar in startdate enddate recordeddate {
		* convert to stata date time
		rename `timevar' rawtime
		gen `timevar' = clock(rawtime, "YMDhms")
		format `timevar' %tc

		* convert to Indonesia time
		gen `timevar'_wib = `timevar'
		replace `timevar'_wib = `timevar'_wib + (13 * 60 * 60 * 1000)
		format `timevar'_wib %tc

		* calculate hour of day
		gen `timevar'Hour = hh(`timevar') + mm(`timevar')/60 + ss(`timevar')/3600
		drop raw
		summ `timevar' `timevar'_wib `timevar'Hour, format

		* create day variable
		gen `timevar'Day = dofc(`timevar')
		format `timevar'Day %td
	}


*********************************
* Format month variables

	la def months 1 "Januari" 2 "Februari" 3 "Maret" 4 "April" ///
				5 "Mei" 6 "Juni" 7 "Juli" 8 "Agustus" ///
				9 "September" 10 "Oktober" 11 "November" 12 "Desember"

	local month_vars dk07_1 ///
					dk07_v2_1 ///
					dk23_1 ///
					kf10_1 ///
					month_batch

	foreach var in `month_vars' {
		replace `var' = "1" if `var' == "Januari"
		replace `var' = "2" if `var' == "Februari"
		replace `var' = "3" if `var' == "Maret"
		replace `var' = "4" if `var' == "April"
		replace `var' = "5" if `var' == "Mei"
		replace `var' = "6" if `var' == "Juni"
		replace `var' = "7" if `var' == "Juli"
		replace `var' = "8" if `var' == "Agustus"
		replace `var' = "8" if `var' == "Augustus"	//spelling mistake in survey :(
		replace `var' = "9" if `var' == "September"
		replace `var' = "10" if `var' == "Oktober"
		replace `var' = "11" if `var' == "November"
		replace `var' = "12" if `var' == "Desember"

		destring `var', replace
		la val `var' months
		tab `var'
		count if `var' == .a
	}


*********************************
* Format Yes/No variables

	la def yesno 0 "No" 1 "Yes", replace

	local yesno_list dem04 ///
					dem06 ///
					dk01 ///
					dk02 ///
					dk12 ///
					dk12_v2 ///
					dk16 ///
					dk18 ///
					dk22 ///
					tr07 ///
					tr08 ///
					tr12 ///
					tr13

	foreach var of varlist `yesno_list' {
	  assert inlist(`var', "Ya", "Tidak", "-99", "")
	  replace `var' = "1" if `var' == "Ya"
	  replace `var' = "0" if `var' == "Tidak"
	  destring `var', replace
	  replace `var' = .a if `var' == -99
	  la val `var' yesno
	  tab `var'
	  count if `var' == .a
	}


*********************************
* Clean rupiah amounts

	local rupiah_list kk01* ///
					kk02* ///
					ka02* ///
					ka03* ///
					ka04* ///
					ka06* ///
					ka07* ///
					ka08* ///
					ka10* ///
					ka11* ///
					ka12* ///
					ka14* ///
					ka15* ///
					ka16* ///
					dk06 ///
					dk06_1 ///
					dk06_v2* ///
					dk15 ///
					dk15_1 ///
					dk15_v2* ///
					mortgage ///
					bought ///
					sold ///
					kf02_1_text ///
					kf02_1_1_text ///
					kf03_1_text ///
					kf03_1_1_text ///
					kf11_5_text ///
					kf11_1_5_text ///
					kf14_1_text ///
					kf14_1_1_text

	foreach i of varlist `rupiah_list' {

		gen `i'_missing = `i' == "-99"

		* Manual replace for common string inputs
		replace `i' = "1000000" if `i' == "1 juta"

		* Clean of characters and periods
		gen `i'_clean = ustrregexra(`i',"[^0-9]","")

		*Drop extremely long responses
		gen `i'_length = strlen(`i'_clean)
		replace `i'_clean="" if `i'_length > 100
		drop `i'_length

		* Replace as missing if response only contained characters
		replace `i'_missing = 1 if missing(`i'_clean) & !missing(`i')
		replace `i' = `i'_clean
		destring `i', replace

		* Record seen but missing responses as .a
		replace `i' = .a if `i'_missing == 1
		drop `i'_missing `i'_clean
	}

	* Replace with second input after prompt if second input is not missing
 	foreach i of varlist kk01 kk02 ka02 ka03 ka04 ka06 ka07 ka08 ka10 ///
		ka11 ka12 ka14 ka15 ka16 kf02 kf03 kf11 kf14 dk06 dk06_v2 dk15 dk15_v2 {
			replace `i' = `i'_1 if !missing(`i'_1)
			drop `i'_1
	}


*********************************
* Clean questions on a scale of 1-6
* If respondents reply with don't know, replace with .d
	local scale_1_6 dk10* ///
					re02* ///
					re03* ///
					gf01 ///
					gf02 ///
					gf03 ///
					gf04 ///
					gf05 ///
					gf06 ///
					gf07 ///
					gf08

	foreach var of varlist `scale_1_6' {
		replace `var' = "1" if strpos(`var', "1") > 0
		replace `var' = "6" if strpos(`var', "6") > 0
		replace `var' = ".d" if strpos(`var', "Tidak tahu") > 0
		destring `var', replace
		replace `var' = .a if `var' == -99
	}


*********************************
* Clean questions on a scale of 0 - 24 months
* Note: should we replace 24+ months with something else?

	local months_1_24 dk08* ///
					  dk17 ///
					  dk19

	foreach var of varlist `months_1_24' {
		replace `var' = "0" if strpos(`var', "dari sebulan") > 0 // less than a month
		replace `var' = "25" if strpos(`var', "dari 24") > 0 // more than 24 months
		destring `var', replace
		replace `var' = .a if `var' == -99
	}

/*----------------------------------------------------*/
 * Section: Drop unwanted responses
/*----------------------------------------------------*/

* Drop variables
	drop time_* treatment month_migration year_migration ///
		month_migration_number ///
		cohort month_for_dk year_for_dk

* Drop test observations
	drop if inlist(recipientlastname, "Test", "test")
	drop if missing(recipientlastname) | missing(recipientfirstname)
	drop if distributionchannel == "preview"
	drop if recipientfirstname == "Test"
	drop if recipientfirstname == "TEST"
	drop if strpos(recipientemail, "example") > 0
	drop if strpos(recipientemail, "test") > 0
	* Drop respondents that were invalidated before validation system was fixed in pilot

* Note: this affects ~40 respondents in v5 in the pilot that were incorrectly invalidated out of the survey
	drop if startdate <= clock("27jul2021 10:30", "DMYhm") & version == 0

* Keep most recent response
* Note: this is done as we sent multiple links in init3/init4 in the pilot
	bysort anon_id: egen complete_time_rank = rank(-startdate)
	keep if complete_time_rank == 1
	drop complete_time_rank

* Keep only verified, completed, consented responses
** DH: we want to keep non-consent answers for LB
	//keep if completed == 1 & consented == 1 & verified == 1

/*----------------------------------------------------*/
* Section: Demographics*/
/*----------------------------------------------------*/

* dem02: gender
* replace with second response if prompted
* note: currently in survey_deidentify: rename to gender_survey
	gen gs = 1 if ver04_1 == "Laki-laki"
	replace gs = 2 if ver04_1 == "Perempuan"
	replace gs = 3 if ver04_1 == "Memilih untuk tidak menjawab"
	replace gs = .a if ver04_1 == "-99" 

	replace ver04 = gs if !missing(gs) & gs != .a
	drop ver04_1 gs

	gen gender_survey = ver04
	la def gender_survey 1 "Male" 2 "Female" 3 "Prefer not to answer", replace
	la val gender_survey gender_survey
	tab gender_survey, m
	la var gender_survey "Gender (survey)"
	drop ver04*

* dem03: marital status
	tab dem03,m
	replace dem03 = "1" if dem03 == "Belum kawin"
	replace dem03 = "2" if dem03 == "Kawin"
	replace dem03 = "3" if dem03 == "Cerai hidup"
	replace dem03 = "4" if dem03 == "Cerai mati"
	destring dem03, replace
	replace dem03 = .a if dem03 == -99
	la def marital_status 1 "Never married" 2 "Married" 3 "Divorced" 4 "Widowed", replace
	la val dem03 marital_status
	la var dem03 "Marital status (survey)"
	rename dem03 marital_status
	tab marital_status, m

* dem04: given birth past two years
	la var dem04 "Given birth in the past 2 years"
	rename dem04 given_birth
	tab given_birth, m

* dem06: Pregant
	la var dem06 "Currently pregnant"
	rename dem06 pregnant
	tab pregnant, m

* dem08: Childcare (multi-select question)
	tab dem08, m
	rename dem08 childcare

	gen childcare_notwork = strpos(childcare, "Saya/pasangan saya tidak bekerja, agar dapat merawat anak") > 0
	gen childcare_workhome = strpos(childcare, "Saya/pasangan saya bekerja dari rumah, agar dapat merawat anak") > 0
	gen childcare_family = strpos(childcare, "Anggota keluarga lainnya yang merawat anak") > 0
	gen childcare_nannyhome = strpos(childcare, "Membayar pengasuh / jasa mengasuh anak untuk merawat anak di rumah") > 0
	gen childcare_nannyout = strpos(childcare, "Membayar pengasuh / jasa mengasuh anak di luar rumah (contoh: Penitipan anak)") > 0
	gen childcare_NA = strpos(childcare, "Tidak memiliki anak dibawah 5 tahun") > 0
	gen childcare_other = strpos(childcare, "Lainnya") > 0

	la var childcare_notwork   "Respondent/spouse care for children less than 5 by not working"
	la var childcare_workhome  "Respondent/spouse care for children less than 5 by working at home"
	la var childcare_family    "Family member cares for children less than 5"
	la var childcare_nannyhome "Nanny cares for children less than 5 at home"
	la var childcare_nannyout  "Nanny cares for children less than 5 outside home"
	la var childcare_NA        "No children less than 5"
	la var childcare_other     "Other child care"
	la var childcare           "If you have kids less than 5 years old, what do you do for child care during the day?"

	foreach var of varlist childcare_* {
		replace `var' = . if childcare == ""
		replace `var' = .a if childcare == "-99"
		la val `var' yesno
	}


/*----------------------------------------------------*/
                  /* Section: kk */
/*----------------------------------------------------*/

* kk01: food expenditure and kk02: non-food expenditure
	rename kk01 food_cons
	la var food_cons "Simple Consumption Qs: Food expenditure last month (rupiah)"

	gen food_cons_adj = food_cons
	replace food_cons_adj = food_sus_l if food_cons < food_sus_l & !mi(food_cons)
	replace food_cons_adj = food_sus_u if food_cons > food_sus_u & !mi(food_cons)
	gen food_cons_adj_l = log(food_cons_adj)
	sum food_cons_adj_l, d
	replace food_cons_adj = food_cons_adj/1000
	sum food_cons_adj, d

	rename kk02 nfood_cons
	la var nfood_cons "Simple Consumption Qs: Non-food expenditure last month (rupiah)"

	gen nfood_cons_adj = nfood_cons
	replace nfood_cons_adj = nfood_sus_l if nfood_cons < nfood_sus_l & !mi(nfood_cons)
	replace nfood_cons_adj = nfood_sus_u if nfood_cons > nfood_sus_u & !mi(nfood_cons)
	gen nfood_cons_adj_l = log(nfood_cons_adj)
	sum nfood_cons_adj_l, d
	replace nfood_cons_adj = nfood_cons_adj/1000
	sum nfood_cons_adj, d

	gen tot_cons_adj = food_cons_adj + nfood_cons_adj
	gen tot_cons_adj_l = log(tot_cons_adj)
	sum tot_cons_adj_l, d
	
	sum tot_cons_adj if completed == 1 & consented == 1 & verified == 1, d
	gen tot_cons_adj_amed = 1 if tot_cons_adj > r(p50) & !mi(tot_cons_adj)
	replace tot_cons_adj_amed = 0 if tot_cons_adj <= r(p50) & !mi(tot_cons_adj)

* kk03 and kk04: ladder questions
	rename kk03 ladder_now
	la var ladder_now "Ladder from best life to worst life: where do you personally feel you stand? (10-top, 1-bottom)"

	rename kk04 ladder_5years
	la var ladder_5years "Ladder from best life to worst life: where do you stand 5 years from now? (10-top, 1-bottom)"

* kk14: food insecurity
	tab kk05, m
	rename kk05 eatless

	replace eatless = "4" if eatless == "Sering"
	replace eatless = "3" if eatless == "Terkadang"
	replace eatless = "2" if eatless == "Jarang"
	replace eatless = "1" if eatless == "Tidak pernah"
	destring eatless, replace

	la def eatless 1 "Never" 2 "Rarely" 3 "Sometimes" 4 "Often"
	la val eatless eatless

	gen eatless_n = (eatless/4)

	gen eatless_often = eatless == 4 if !missing(eatless)
	gen eatless_some = eatless == 3 if !missing(eatless)
	gen eatless_rarely = eatless == 2 if !missing(eatless)
	gen eatless_never = eatless == 1 if !missing(eatless)

	foreach var of varlist eatless* {
	  replace `var' = .a if eatless == -99
	  la val `var' yesno
	}

	la var eatless_often  "Often eat less than usual in past 3 months due to money"
	la var eatless_some   "Sometimes eat less than usual in past 3 months due to money"
	la var eatless_rarely "Rarely eat less than usual in past 3 months due to money"
	la var eatless_never  "Never eat less than usual in past 3 months due to money"
	la var eatless        "Over the past 3 months, how often did you eat less due to lack of money?"

	summ eatless*

** kk06: consumption smoothing activities (multi-select)
	tab kk06, m
	rename kk06 consmooth

	gen consmooth_savings = strpos(consmooth, "Menggunakan tabungan") > 0
	gen consmooth_loans = strpos(consmooth, "Meminjam uang") > 0
	gen consmooth_sell = strpos(consmooth, "Menjual/menggadaikan barang yang dimiliki") > 0
	gen consmooth_paidless = strpos(consmooth, "Melakukan pekerjaan dengan gaji lebih rendah") > 0
	gen consmooth_freelance = strpos(consmooth, "Menjadi pekerja lepas") > 0
	gen consmooth_reduce = strpos(consmooth, "Mengurangi pengeluaran") > 0
	gen consmooth_gov_aid = strpos(consmooth, "Mendaftar program bantuan di kantor pemerintah") > 0
	gen consmooth_prakerja = strpos(consmooth, "kartu Prakerja") > 0
	gen consmooth_BLT_UMKM = strpos(consmooth, "BLT UMKM") > 0
	gen consmooth_move = strpos(consmooth, "Pindah tempat tinggal") > 0
	gen consmooth_NA = strpos(consmooth, "Tidak melakukan aktivitas apapun") > 0
	gen consmooth_other = strpos(consmooth, "Lainnya") > 0

	foreach var of varlist consmooth_* {
		replace `var' = .a if consmooth == "-99"
		la val `var' yesno
	}

	la var consmooth_savings      "Used savings to overcome financial difficulties"
	la var consmooth_loans        "Took out loans to overcome financial difficulties"
	la var consmooth_sell         "Sold assets to overcome financial difficulties"
	la var consmooth_paidless 	  "Took job that pays less to overcome financial difficulties"
	la var consmooth_freelance    "Did freelance work to overcome financial difficulties"
	la var consmooth_reduce       "Reduced spending to overcome financial difficulties"
	la var consmooth_gov_aid      "Applied for government aid to overcome financial difficulties"
	la var consmooth_prakerja     "Applied for Kartu Prakerja to overcome financial difficulties"
	la var consmooth_BLT_UMKM     "Applied for BLT micro and small business assistance (BPUM) to overcome financial difficulties"
	la var consmooth_move         "Moved locations to overcome financial difficulties"
	la var consmooth_NA           "Did not experience financial difficulties"
	la var consmooth_other        "Other activity to overcome financial difficulties"
	la var consmooth              "Over the past 3 months, what did you do to overcome financial difficulties?"

	summ consmooth_*
	drop consmooth

	rename kk06_10_text consmooth_text
	la var consmooth_text "Over the past 3 months, what did you do to overcome financial difficulties? (Other, open-ended)"


/*----------------------------------------------------*/
                  /* Section: ka */
/*----------------------------------------------------*/

* KA01 - KA16: Asset transacations
	tab ka01, m

	rename ka01 bike
	rename ka02 bike_buy_val
	rename ka03 bike_sell_val
	rename ka04 bike_lend_val

	rename ka05 devices
	rename ka06 devices_buy_val
	rename ka07 devices_sell_val
	rename ka08 devices_lend_val

	rename ka09 gold
	rename ka10 gold_buy_val
	rename ka11 gold_sell_val
	rename ka12 gold_lend_val

	rename ka13 livestock
	rename ka14	livestock_buy_val
	rename ka15 livestock_sell_val
	rename ka16 livestock_lend_val

	foreach var of varlist bike devices gold livestock {
		gen `var'_buy = strpos(`var', "Ya, membeli") > 0 if `var' != ""
		gen `var'_sell = strpos(`var', "Ya, menjual") > 0 if `var' != ""
		gen `var'_lend = strpos(`var', "Ya, menggadaikan") > 0 if `var' != ""
		gen `var'_none = strpos(`var', "Tidak pernah") > 0 if `var' != ""

		foreach dummy of varlist `var'_* {
			replace `dummy' = .a if `var' == "-99"
			la val `dummy' yesno
		}
	}

	foreach var in buy sell lend {
		egen any_`var' = rowtotal(*_`var')
		replace any_`var' = 1 if any_`var' > 0
		la val any_`var' yesno
	}

	summ *buy *sell *lend *none

	la var bike_buy 			"Bought a motorbike"
	la var bike_sell			"Sold a motorbike"
	la var bike_lend			"Lent a motorbike"
	la var bike_none			"Didn't buy, sell or lend a motorbike"
	la var bike_buy_val 		"Value of Motorbike bought"
	la var bike_sell_val 		"Value of Motorbike sold"
	la var bike_lend_val 		"Value of Motorbike mortgaged"

	la var devices_buy 			"Bought devices"
	la var devices_sell			"Sold devices"
	la var devices_lend			"Lent devices"
	la var devices_none			"Didn't buy, sell or lend devices"
	la var devices_buy_val  	"Value of Electronic devices bought"
	la var devices_sell_val 	"Value of Electronic devices sold"
	la var devices_lend_val 	"Value of Electronic devices mortgaged"

	la var gold_buy 			"Bought gold or jewelry"
	la var gold_sell			"Sold gold or jewelry"
	la var gold_lend			"Lent gold or jewelry"
	la var gold_none			"Didn't buy, sell or lend gold or jewelry"
	la var gold_buy_val 		"Value of gold/jewelry bought"
	la var gold_sell_val 		"Value of gold/jewelry sold"
	la var gold_lend_val  		"Value of gold/jewelry mortgaged"

	la var livestock_buy 		"Bought livestock"
	la var livestock_sell		"Sold livestock"
	la var livestock_lend		"Lent livestock"
	la var livestock_none		"Didn't buy, sell or lend livestock"
	la var livestock_buy_val  	"Value of livestock bought"
	la var livestock_sell_val 	"Value of livestock sold"
	la var livestock_lend_val 	"Value of livestock mortgaged"

	la var any_buy 				"Bought any assets (bike, devices, gold, livestock)"
	la var any_sell				"Sold any assets (bike, devices, gold, livestock)"
	la var any_lend				"Lent any assets (bike, devices, gold, livestock)"

	drop bike devices gold livestock


/*----------------------------------------------------*/
                  /* Section: sk */
/*----------------------------------------------------*/

* sk01: Loans
	tab sk01, m
	rename sk01 loan

	gen loan_borrow = strpos(loan,"Meminjam uang") > 0
	gen loan_get = strpos(loan,"Menerima pemberian uang") > 0
	gen loan_lend = strpos(loan,"Meminjamkan uang") > 0
	gen loan_give = strpos(loan,"Memberikan uang") > 0
	gen loan_NA = strpos(loan,"Tidak melakukan") > 0

	foreach var of varlist loan_* {
		replace `var' = .a if loan == "-99"
		la val `var' yesno
	}

	la var loan_borrow      "Taken additional loans"
	la var loan_get  		"Received gifts"
	la var loan_lend        "Given loans"
	la var loan_give 		"Gave gifts"
	la var loan_NA          "Did not do any loan activites"
	la var loan             "Which of the following loan activities have you done?"

	summ loan_*

* sk02: rupiah amount variables
	local categories `" 1 "Kurang dari Rp 99 ribu" 2 "Rp 100 ribu - 249 ribu" 3 "Rp 250 ribu - 499 ribu" 4 "Rp 500 ribu - 999 ribu"	5  "Rp 1 juta - 2.49 juta" 6 "Rp 2.5 juta - 4.99 juta" 7 "Rp 5 juta - 9.99 juta" 8 "Lebih dari Rp 10 juta" "'

	tokenize `categories'
	la def categories `categories', replace

	rename sk02 loan_borrow_amt
	rename sk05 loan_get_amt
	rename sk07 loan_lend_amt
	rename sk09 loan_give_amt

	foreach var of varlist loan_borrow_amt loan_get_amt loan_lend_amt loan_give_amt {
		* fix slight difference in response text between questions
		replace `var' = "Lebih dari Rp 10 juta" if `var' == "Lebih dari 10 juta"
		replace `var' = "Rp 1 juta - 2.49 juta" if `var' == "Rp 1 juta - 2,49 juta"
		replace `var' = "Rp 2.5 juta - 4.99 juta" if `var' == "Rp 2,5 juta - 4,99 juta"
		replace `var' = "Rp 2.5 juta - 4.99 juta" if `var' == "Rp 2,5 juta - Rp 4,99 juta"
		replace `var' = "Rp 2.5 juta - 4.99 juta" if `var' == "Rp 2.5 juta - Rp 4.99 juta"
		replace `var' = "Rp 5 juta - 9.99 juta" if `var' == "Rp 5 juta - 9,99 juta"
		replace `var' = "Rp 5 juta - 9.99 juta" if `var' == "Rp 5 juta - 9,99"
		replace `var' = "Rp 5 juta - 9.99 juta" if `var' == "Rp 5 juta - 9.99"

		local x = 1
		local y = 2
		forval i = 1 / 8 {
			replace `var' = "``x''" if `var' == "``y''"
			local x = `x' + 2
			local y = `y' + 2
		}

		destring `var', replace
		replace `var' = .a if `var' == -99
		la val `var' categories
	}

	la var loan_borrow_amt 	"How much have you/your household borrowed?"
	la var loan_get_amt 	"What is the value of all the gifts that you/your household received?"
	la var loan_lend_amt 	"How much did you/your household lend?"
	la var loan_give_amt 	"How much did you/your household give, in gifts?"

* transaction medium
	tab sk04, m

	rename sk04 borrow
	rename sk06 get
	rename sk08 lend
	rename sk10 give

	foreach i of varlist borrow get lend give {
		gen `i'_cash = strpos(`i',"Melalui uang tunai") > 0
		gen `i'_bank = strpos(`i',"Melalui transfer bank") > 0
		gen `i'_ewallet = strpos(`i',"Melalui transfer e-wallet") > 0
		gen `i'_item = strpos(`i',"barang fisik") > 0
		gen `i'_other = strpos(`i',"Lainnya") > 0

		foreach dummy of varlist `i'_* {
			replace `dummy' = .a if `i' == "-99"
			lab val `dummy' yesno
		}
	}

	la var borrow_cash 		"Method of borrowing money: cash"
	la var borrow_bank 		"Method of borrowing money: bank"
	la var borrow_ewallet 	"Method of borrowing money: e-wallet"
	la var borrow_item 		"Method of borrowing money: items"
	la var borrow_other 	"Method of borrowing money: other"

	la var get_cash 		"Method of receiving gifts: cash"
	la var get_bank 		"Method of receiving gifts: bank"
	la var get_ewallet 		"Method of receiving gifts: e-wallet"
	la var get_item 		"Method of receiving gifts: items"
	la var get_other 		"Method of receiving gifts: other"

	la var lend_cash 		"Method of giving out loans: cash"
	la var lend_bank 		"Method of giving out loans: bank"
	la var lend_ewallet 	"Method of giving out loans: e-wallet"
	la var lend_item 		"Method of giving out loans: items"
	la var lend_other 		"Method of giving out loans: other"

	la var give_cash 		"Method of giving gifts: cash"
	la var give_bank 		"Method of giving gifts: bank"
	la var give_ewallet 	"Method of giving gifts: e-wallet"
	la var give_item 		"Method of giving gifts: items"
	la var give_other 		"Method of giving gifts: other"

	summ *cash *bank *ewallet *item *other
	drop loan borrow get lend give

* sk03: from whom did you borrow
	tab sk03, m
	rename sk03 loan_from

	gen loan_from_family = strpos(loan_from, "Keluarga") > 0
	gen loan_from_friends = strpos(loan_from, "Teman") > 0
	gen loan_from_banks = strpos(loan_from, "Bank") > 0
	gen loan_from_microfin = strpos(loan_from, "Lembaga keuangan mikro") > 0
	gen loan_from_moneylend = strpos(loan_from, "Perorangan lainnya dengan bunga") > 0
	gen loan_from_other = strpos(loan_from, "Lainnya") > 0

	foreach i of varlist loan_from_* {
		replace `i' = .a if loan_from == "-99"
		la val `i' yesno
	}

	summ loan_from_*

	la var loan_from 			"From whom did you/your household borrow?"
	la var loan_from_family 	"Household borrowed from family"
	la var loan_from_friends 	"Household borrowed from friends"
	la var loan_from_banks 		"Household borrowed from banks"
	la var loan_from_microfin 	"Household borrowed from microfinance organization"
	la var loan_from_moneylend 	"Household borrowed from moneylenders"
	la var loan_from_other 		"Household borrowed from other"


/*----------------------------------------------------*/
                  /* Section: dk */
/*----------------------------------------------------*/

*dk01: work last week
	la var dk01 "Work for pay/assist business/own business in the past week"
	rename dk01 employed
	tab employed, m

* dk02: temporarily not working
	la var dk02 "Have a job/own a business, but not work in the past week"
	rename dk02 temp_no_work
	gen temp_no_work_missing = (temp_no_work == .a)
	replace temp_no_work = 0 if missing(temp_no_work)
	replace temp_no_work = . if (temp_no_work_missing == 1)
	tab temp_no_work, m
	drop temp_no_work_missing

* KBLI and KBJI codes (dk03-4 and dk20-21)
	foreach i of varlist dk03_? dk04_? {
		encode `i', gen(`i'_encode)
		replace `i'_encode = .a if `i' == "-99"
		drop `i'
		rename `i'_encode `i'
		tab `i', m
	}

	rename dk03_1 KBLI_1
	rename dk03_2 KBLI_2
	rename dk03_3 KBLI_3
	rename dk04_1 KBJI_1
	rename dk04_2 KBJI_2

	la var KBLI_1 "KBLI 1-digit main job (Industry Category)"
	la var KBLI_2 "KBLI 2-digit main job (Industry)"
	la var KBLI_3 "KBLI 2-digit main job (Sub-Industry)"
	la var KBJI_1 "KBJI 1-digit main job"
	la var KBJI_2 "KBJI 2-digit main job"

* Encode the values of dk20/dk21 with previous labels defined from dk03/dk04
	encode dk20_1, gen(dk20_1_encode) label(dk03_1)
	encode dk20_2, gen(dk20_2_encode) label(dk03_2)
	encode dk20_3, gen(dk20_3_encode) label(dk03_3)
	encode dk21_1, gen(dk21_1_encode) label(dk04_1)
	encode dk21_2, gen(dk21_2_encode) label(dk04_2)

	foreach i of varlist dk20_? dk21_? {
		replace `i'_encode = .a if `i' == "-99"
		drop `i'
		rename `i'_encode `i'
	}

	rename dk20_1 KBLI_1_feb21
	rename dk20_2 KBLI_2_feb21
	rename dk20_3 KBLI_3_feb21
	rename dk21_1 KBJI_1_feb21
	rename dk21_2 KBJI_2_feb21

	la var KBLI_1_feb21 "KBLI 1-digit Feb. 2021 job (Industry Category)"
	la var KBLI_2_feb21 "KBLI 2-digit Feb. 2021 job (Industry)"
	la var KBLI_3_feb21 "KBLI 2-digit Feb. 2021 job (Sub-Industry)"
	la var KBJI_1_feb21 "KBJI 1-digit Feb. 2021 job"
	la var KBJI_2_feb21 "KBJI 2-digit Feb. 2021 job"

* Industry switch dummy
	forvalues i = 1/3{
		gen switch_industry_`i' = KBLI_`i' != KBLI_`i'_feb21 if !mi(KBLI_`i', KBLI_`i'_feb21)
	}

* dk05 - dk10: combine two branches of survey questions (randomized ordering of questions)
	foreach var of varlist dk05 dk06 dk08 dk10 dk11 dk12 dk13 dk15 {
		assert missing(`var') if !missing(`var'_v2)
		replace `var' = `var'_v2 if !missing(`var'_v2)
		drop `var'_v2
	}

	forval i = 1/2 {
		assert missing(dk07_`i') if !missing(dk07_v2_`i')
		replace dk07_`i' = dk07_v2_`i' if !missing(dk07_v2_`i')
		drop dk07_v2_`i'
	}

	forval i = 1/7 {
		foreach dk in dk09 dk14{
			assert missing(`dk'_1_`i') if !missing(`dk'_v2_1_`i')
			replace `dk'_1_`i' = `dk'_v2_1_`i' if !missing(`dk'_v2_1_`i')
			drop `dk'_v2_1_`i'
		}
	}

* dk05 and dk13: employment status
	tab dk05, m
	rename dk05 employ_status
	rename dk13 employ_status2

	label define emp 1 "Self-employed" ///
				 2 "Laborer/permanent employee" ///
				 3 "Laborer/temporary employee or freelance" ///
				 4 "Business owner" ///
				 5 "Family/unpaid worker"

	foreach var in employ_status employ_status2 {
		replace `var' = "1" if strpos(`var', "Berusaha sendiri") > 0
		replace `var' = "2" if strpos(`var', "Buruh/karyawan/pegawai tetap") > 0
		replace `var' = "3" if strpos(`var', "Buruh/karyawan/pegawai temporer atau") > 0
		replace `var' = "3" if strpos(`var', "Buruh/karyawan/pegawai lepas atau") > 0
		replace `var' = "4" if strpos(`var', "Pemilik Wirausaha") > 0
		replace `var' = "5" if strpos(`var', "Pekerja keluarga/tidak dibayar") > 0

		destring `var', replace
		replace `var' = .a if `var' == -99
		la val `var' emp
	}

	la var employ_status "Employment status in main job"
	la var employ_status2 "Employment status in second job"

	gen self_emp = employ_status == 1
	gen bus_owner = employ_status == 4
	gen perm_emp = employ_status == 2
	gen temp_emp = employ_status == 3
	gen fam_unpaid = employ_status == 5
	gen self_emp_bus_owner = (employ_status == 1 | employ_status == 4) 
	
	* missing if non-missing employed but missing the employ_status
	foreach var of varlist self_emp bus_owner perm_emp temp_emp fam_unpaid self_emp_bus_owner {
		replace `var' = .a if employ_status == .a
	}

	la val self_emp yesno
	la val bus_owner yesno
	la val perm_emp yesno
	la val temp_emp yesno
	la val fam_unpaid yesno
	la val self_emp_bus_owner yesno

	la var self_emp           "Self-employed"
	la var bus_owner          "Business owner"
	la var perm_emp           "Permanent employee"
	la var temp_emp           "Temporary employee"
	la var fam_unpaid         "Working with family/unpaid worker"
	la var self_emp_bus_owner "Self-employed OR Business owner"

* dk06 and dk15: income
	rename dk06 income
	la var income "Income from main job (in rupiah)"

	rename dk15 income2
	la var income2 "Income from secondary job (in rupiah)"

	egen inc_all = rowtotal(income income2) if !(mi(income) & mi(income2))
	replace inc_all = 0 if employed == 0

* Winsorize incomes
	gen income_adj = inc_all
	replace income_adj = inc_sak_u if income_adj > inc_sak_u & !mi(income_adj)
	replace income_adj = inc_sak_l if income_adj < inc_sak_l & !mi(income_adj) & income_adj !=0
	replace income_adj = income_adj/1000

	gen income_positive = income_adj > 0 if !mi(income_adj)

* create separate date variable (assume middle of month for start date average)
	gen job_startdate = mdy(dk07_1, 15, dk07_2)
	replace job_startdate = .a if dk07_2 == .a | dk07_1 == .a
	format job_startdate %td
	summ job_startdate, format
	la var job_startdate  "Start date of job or business activity"
	drop dk07*

* new job since april 2020
	gen new_job = (mdy(month(job_startdate), 1, year(job_startdate)) - mdy(4, 1, 2020)) >= 0 if !mi(job_startdate) & employ_status != 4
	replace new_job = 0 if employ_status == 4 & !mi(job_startdate)
	replace new_job = 0 if employed == 0
	tab new_job, m

* calculate current job tenure
	gen job_tenure = startdateDay - job_startdate
	la var job_tenure "Tenure of current job (in days)"

* dk08: period of job search (should we re-label more than 24 months as something else?)
	tab dk08, m
	rename dk08 job_search_time
	la var job_search_time "Months it took to find/prepare current main job/business"

* dk09 and dk14: hours worked
	summ dk09* dk14*

	local days = "mon tue wed thu fri sat sun"
	tokenize `days'

	forval i = 1/7 {
		gen jobhours_``i'' = dk09_1_`i'
		gen jobhours2_``i'' = dk14_1_`i'
	}

	drop dk09_1* dk14_1*

	* if respondent leaves one day blank but answers other days, assume this is a 0, not missing
	egen hour_nonmiss = rownonmiss(jobhours_*)

	foreach var of varlist jobhours_* {
		replace `var' = 0 if missing(`var') & hour_nonmiss > 0
	}

	egen hour_nonmiss2 = rownonmiss(jobhours2_*)

	foreach var of varlist jobhours2_* {
		replace `var' = 0 if missing(`var') & hour_nonmiss2 > 0
	}

	drop hour_nonmiss*

	* total hours per week
	egen jobhours = rowtotal(jobhours*) , missing

	replace jobhours = .a if jobhours_mon == .a ///
							& jobhours_tue == .a ///
							& jobhours_wed == .a ///
							& jobhours_thu == .a ///
							& jobhours_fri == .a ///
							& jobhours_sat == .a ///
							& jobhours_sun == .a

	la var jobhours "Hours worked in main job in past week"

	egen jobhours2 = rowtotal(jobhours2*) , missing

	replace jobhours2 = .a if jobhours2_mon == .a ///
							& jobhours2_tue == .a ///
							& jobhours2_wed == .a ///
							& jobhours2_thu == .a ///
							& jobhours2_fri == .a ///
							& jobhours2_sat == .a ///
							& jobhours2_sun == .a

	la var jobhours2 "Hours worked in secondary job in past week"

	summ jobhours*
	summ jobhours2*

* total hours worked in last week among all jobs;
	egen jobhours_total = rowtotal(jobhours jobhours2) if !(mi(jobhours) & mi(jobhours2))
	replace jobhours_total = 0 if employed == 0
	sum jobhours_total, d

// 210 hours in a week is not possible, so we truncate
	gen jobhours_adj = jobhours_total
	*Winsorize
	replace jobhours_adj = hrs_sak_u if jobhours_adj > hrs_sak_u & !mi(jobhours_adj)
	sum jobhours_adj, d

	la var jobhours_adj "Total hours worked in the past week"

* wages
	gen wage = (income_adj/31)/(jobhours_adj/7)
	replace wage = 0 if jobhours_adj == 0
	sum wage, d
	gen wage_adj = wage
	*Winsorize
	replace wage_adj = wage_sak_u if wage_adj > wage_sak_u & !mi(wage_adj)
	replace wage_adj = wage_sak_l if wage_adj < wage_sak_l & !mi(wage_adj) & wage_adj !=0
	sum wage_adj, d

	gen wage_positive = wage_adj > 0 if !mi(wage_adj)

* dk10: job satisfaction
	tab dk10, m
	rename dk10 job_satisfy
	la var job_satisfy "Satisfaction with current main job (on a scale of 1-6, 1 = completely dissatisfied, 6 = completely satisfied)"

	gen job_satisfy_n = (job_satisfy/6)

* dk11: internet use at job
	tab dk11
	gen job_web_promo = strpos(dk11, "Ya, untuk promosi") > 0
	gen job_web_comm = strpos(dk11, "Ya, untuk komunikasi") > 0
	gen job_web_buy_sell = strpos(dk11, "Ya, untuk menjual/membeli barang") > 0
	gen job_web_info = strpos(dk11, "Ya, untuk mencari informasi online") > 0
	gen job_web_not_use = strpos(dk11, "Tidak, saya tidak menggunakan internet pada pekerjaan ini") > 0

	foreach var of varlist job_web_promo job_web_comm job_web_buy_sell job_web_info job_web_not_use {
		replace `var' = .a if dk11 == "-99"
		destring `var', replace
	}

	drop dk11
	summ job_web*

	la var job_web_promo "Use internet on the job for promotion"
	la var job_web_comm "Use internet on the job for communication"
	la var job_web_buy_sell "Use internet on the job to sell/buy items"
	la var job_web_info "Use internet on the job to search for information online"
	la var job_web_not_use "Don't use internet on the job"

	egen job_web = rowtotal(job_web_*)
	replace job_web = 1 if job_web > 0

	la var job_web "Use internet on the job"
	la val job_web yesno

* dk12: multiple jobs
	tab dk12, m
	rename dk12 multiple_jobs
	replace multiple_jobs = 0 if multiple_jobs == .
	la var multiple_jobs "Had more than one job in past week"
	tab multiple_jobs, m

* dk16: searching for job
	rename dk16 job_search
	la var job_search "In the past week, are you actively searching for a job?"
	tab job_search, m

* dk17: How many months have you been searching for a job?
	tab dk17, m
	rename dk17 job_search_time_curr
	la var job_search_time_curr "Months spent looking for job (if currently actively looking for a job)"

* dk18: preparing to start new business
	rename dk18 prep_new_bus
	la var prep_new_bus "Preparing to start a new business in last week"
	tab prep_new_bus, m

* dk19: how long preparing to start new business
	tab dk19, m
	rename dk19 prep_new_bus_time
	la var prep_new_bus_time "Months spent prepping new business"

* dk22: HH member started business
	rename dk22 hh_new_bus
	la var hh_new_bus "Household member started a business"
	tab hh_new_bus, m

* dk23: when did your HH member's business start (assume middle of month for dates)
	gen hh_new_bus_start = mdy(dk23_1, 15, dk23_2)
	format hh_new_bus_start %td

	summ hh_new_bus_start, format
	la var hh_new_bus_start "Household member's new business start date'"

	drop dk23*

/*----------------------------------------------------*/
 * Section 12: MI - Format migration questions
/*----------------------------------------------------*/


 *********** DON'T FORGET TO INSERT MIGRATION CLEANING CODE
 * Incorporate from 1 survey deidentify
 * organize PODES data
*************************************************************


/*----------------------------------------------------*/
                  /* Section: re */
/*----------------------------------------------------*/

* re01: Why, in your opinion, are there people in this country who live in need?
	tab re01, m
	replace re01 = "1" if re01 == "Kemiskinan disebabkan oleh kemalasan dan kurangnya tekad/motivasi"
	replace re01 = "0" if re01 == "Kemiskinan disebabkan oleh kondisi sosial yang kurang adil"
	destring re01, replace
	replace re01 = .a if re01 == -99
	la var re01 "Why, in your opinion, are there people in this country who live in need?"
	la def re01 1 "Poor because of laziness and lack of willpower" 0 "Poor because of an unfair society", replace
	la val re01 re01
	rename re01 views_why_poor
	tab views_why_poor, m

* re02: The government vs each person in Indonesia should do everything to improve the standard of living of all poor Indonesian
	tab re02 re02_2, m
	gen views_gov_help = re02
	replace views_gov_help = re02_2 if missing(re02)
	tab views_gov_help, m
	drop re02 re02_2
	replace views_gov_help = 7-views_gov_help
	la var views_gov_help  "Government responsibility to help the poor (1 - Individual has highest responsibility, 6 - Govt has highest responsibility)"
	gen views_gov_help_n = views_gov_help/6

* re03: allocate social assistance budget to the poor and those most in need vs even distribution
	tab re03 re03_2, m
	gen views_allocation = re03
	replace views_allocation = re03_2 if missing(re03)
	tab views_allocation, m
	drop re03 re03_2
	replace views_allocation = 7-views_allocation
	la var views_allocation  "Allocation of social assistance (1 - equal distribution, 6 - poor get the most)"
	gen views_allocation_n = views_allocation/6

* re04 and re05: public opinion of government
	tab re04, m
	la def satisfy_4_scale 1 "Not Satisfied" 2 "Somewhat Satisfied" 3 "Moderately Satisfied" 4 "Very satisfied", replace

	foreach var in re04 re05 {
		replace `var' = "4" if `var' == "Sangat puas"
		replace `var' = "3" if `var' == "Puas"
		replace `var' = "2" if `var' == "Agak tidak puas"
		replace `var' = "1" if `var' == "Sangat tidak puas"
		destring `var', replace
		replace `var' = .a if `var' == -99
		la val `var' satisfy_4_scale
		tab `var'
	}

	rename re04 views_gov_ph
	rename re05 views_gov_crisis

	la var views_gov_ph  "Government response to COVID-19, in terms of public health"
	la var views_gov_crisis "Government response to COVID-19, in terms of economic response)"

	gen views_gov_ph_n = views_gov_ph/4
	gen views_gov_crisis_n = views_gov_crisis/4


/*----------------------------------------------------*/
                  /* Section: tr */
/*----------------------------------------------------*/

* WILLINGNESS TO PAY
* tr01-tr06: Scenario for credit vs cash
	la var tr01 "If the program were expanded, would you prefer: 500K scholarship vs. 300K post-training incentive?"
	la var tr02 "If the program were expanded, would you prefer: 500K scholarship vs. 400K post-training incentive?"
	la var tr03 "If the program were expanded, would you prefer: 500K scholarship vs. 500K post-training incentive?"
	la var tr04 "If the program were expanded, would you prefer: 500K scholarship vs. 600K post-training incentive?"
	la var tr05 "If the program were expanded, would you prefer: 500K scholarship vs. 200K post-training incentive?"
	la var tr06 "If the program were expanded, would you prefer: 500K scholarship vs. 100K post-training incentive?"

	la def pk_creditvscash1 1 "500K scholarship" 0 "300K post-training incentive", modify
	la def pk_creditvscash2 1 "500K scholarship" 0 "400K post-training incentive", modify
	la def pk_creditvscash3 1 "500K scholarship" 0 "500K post-training incentive", modify
	la def pk_creditvscash4 1 "500K scholarship" 0 "600K post-training incentive", modify
	la def pk_creditvscash5 1 "500K scholarship" 0 "200K post-training incentive", modify
	la def pk_creditvscash6 1 "500K scholarship" 0 "100K post-training incentive", modify

	forval i=1/6 {
		replace tr0`i' = "1" if strpos(tr0`i', "beasiswa") > 0
		replace tr0`i' = "1" if strpos(tr0`i', "tambahan bantuan pelatihan") > 0
		replace tr0`i' = "0" if strpos(tr0`i', "insentif") > 0
		replace tr0`i' = "0" if strpos(tr0`i', "tambahan bantuan secara tunai") > 0
		destring tr0`i', replace
		replace tr0`i' = .a if tr0`i' == -99
		la val tr0`i' pk_creditvscash`i'
		tab tr0`i', m
	}

* tr07: Current training history
	rename tr07 take_courses
	replace take_courses = 1 if tr08 ==1
	la var take_courses "Currently taking training courses"
	tab take_courses, m

* tr08: kp-Specific training history
	rename tr08 take_kp_courses
	la var take_kp_courses "Taken Kartu Prakerja courses"
	tab take_kp_courses, m

* tr09: Number of courses taken
	cap drop num_courses
	tab tr09
	rename tr09 num_courses
	destring num_courses, replace
	replace num_courses = 0 if missing(num_courses)
	replace num_courses = .a if num_courses == -99
	la var num_courses "Number of courses taken"

* tr10: Hours spent on cours
	rename tr10 course_hours
	replace course_hours = 0 if mi(course_hours)
	la var course_hours "Hours of Training"

* tr11: training categories
	tab tr11, m
	replace tr11="1" if strpos(tr11,"Teknik") > 0
	replace tr11="2" if strpos(tr11,"Teknologi") > 0
	replace tr11="3" if strpos(tr11,"Keuangan") > 0
	replace tr11="4" if strpos(tr11,"Perkantoran") > 0
	replace tr11="5" if strpos(tr11,"Manajemen") > 0
	replace tr11="6" if strpos(tr11,"Gaya") > 0
	replace tr11="7" if strpos(tr11,"Pertanian") > 0
	replace tr11="8" if strpos(tr11,"Makanan") > 0
	replace tr11="9" if strpos(tr11,"Penjualan") > 0
	replace tr11="10" if strpos(tr11,"Bahasa Asing") > 0
	replace tr11="11" if strpos(tr11,"Sosial") > 0
	replace tr11="95" if strpos(tr11,"Lainnya") > 0
	destring tr11, replace
	replace tr11 = .a if tr11 == -99

	la def tr11 1 "Engineering" ///
				2 "IT" ///
				3 "Finance" ///
				4 "Office Admin" ///
				5 "Management" ///
				6 "Lifestyle" ///
				7 "Agriculture" ///
				8 "F&B" ///
				9 "Sales&Marketing" ///
				10 "Foreign language" ///
				11 "Sociobehavioral" ///
				95 "Others", replace
	la val tr11 tr11
	la var tr11 "What is the category of the most recent course you have taken?"
	tab tr11, m

* tr12- tr13: certificate ownership and use
	rename tr12 course_cert
	la var course_cert  "Took a course that gave a course certificate"
	replace course_cert = 0 if course_cert == .
	tab course_cert, m

	rename tr13 course_cert_use
	la var course_cert_use "Used any course certificate when looking for a job"
	replace course_cert_use = 0 if course_cert_use == .
	tab course_cert_use, m

* tr14: Pay for training
	tab tr14, m
	replace tr14="1" if strpos(tr14,"tempat kerja") > 0
	replace tr14="2" if strpos(tr14,"Pemerintah") > 0
	replace tr14="3" if strpos(tr14,"Kartu Prakerja") > 0
	replace tr14="4" if strpos(tr14,"pribadi") > 0
	replace tr14="5" if strpos(tr14,"keluarga") > 0
	replace tr14="6" if strpos(tr14,"Pinjaman") > 0
	replace tr14="7" if strpos(tr14,"tidak berbayar") > 0
	replace tr14="95" if strpos(tr14,"Lainnya") > 0
	destring tr14, replace
	replace tr14 = .a if tr14 == -99

	la def tr14 1 "Employers" ///
				2 "Government" ///
				3 "Kartu Prakerja" ///
				4 "Personal" ///
				5 "Relative" ///
				6 "Credit" ///
				7 "Free" ///
				95 "Others", replace

	la val tr14 tr14
	la var tr14 "Payment method for most recent course"
	tab tr14, m

	gen course_pay_kp = tr14 == 3
	la var course_pay_kp "Used Kartu Prakerja to pay for any course"
	la val course_pay_kp yesno
	tab course_pay_kp, m

* tr15: Mode of training
	tab tr15, m
	replace tr15="1" if strpos(tr15,"tanpa interaksi langsung") > 0
	replace tr15="2" if strpos(tr15,"dengan interaksi langsung") > 0
	replace tr15="3" if strpos(tr15,"Offline/tatap muka") > 0
	destring tr15, replace
	replace tr15 = .a if tr15 == -99

	la def tr15 1 "Online, with no live interaction" ///
				2 "Online, with live interaction" ///
				3 "In person", replace

	la val tr15 tr15
	la var tr15 "Delivery method of most recent course"
	tab tr15, m

	gen course_online = (tr15 == 1 | tr15 == 2)
	la var course_online "Took at least one course online"
	la val course_online yesno
	tab course_online, m

	gen course_online_live = (tr15 == 2)
	la var course_online_live "Took at least one course online with live instruction"
	la val course_online_live yesno
	tab course_online_live, m

	gen course_offline = (tr15 == 3)
	la var course_offline "Took at least one course offline"
	la val course_offline yesno
	tab course_offline, m


/*----------------------------------------------------*/
                  /* Section: gf */
/*----------------------------------------------------*/

* gf01-gf08: Comfort with utilizing e-wallet and bank account
	la var gf01 "Comfort with: Signing up for a new e-wallet account (1-not comfortable, 6-very comfortable)"
	la var gf02 "Comfort with: Depositing/withdrawing to/from e-wallet (1-not comfortable, 6-very comfortable)"
	la var gf03 "Comfort with: Using an e-wallet for online transactions (1-not comfortable, 6-very comfortable)"
	la var gf04 "Comfort with: Using e-wallet for transactions with agent/shop (1-not comfortable, 6-very comfortable)"
	la var gf05 "Comfort with: Signing up for a new bank account (1-not comfortable, 6-very comfortable)"
	la var gf06 "Comfort with: Depositing/withdrawing to/from bank account (1-not comfortable, 6-very comfortable)"
	la var gf07 "Comfort with: Using a debit card for online transactions (1-not comfortable, 6-very comfortable)"
	la var gf08 "Comfort with: Using debit card for transactions with agent/shop (1-not comfortable, 6-very comfortable)"

    rename gf01 comf_e_signup
	rename gf02 comf_e_cashout
	rename gf03 comf_e_online
	rename gf04 comf_e_offline
	rename gf05 comf_b_signup
	rename gf06 comf_b_cashout
	rename gf07 comf_b_online
	rename gf08 comf_b_offline

* gf09: between e-wallets and bank accounts, which is more secure
* .d for don't know
	tab gf09, m
	replace gf09 = "1" if gf09 == "E-wallet jauh lebih aman daripada rekening bank"
	replace gf09 = "2" if gf09 == "E-wallet sedikit lebih aman daripada rekening bank"
	replace gf09 = "3" if gf09 == "Rekening bank sedikit lebih aman daripada e-wallet"
	replace gf09 = "4" if gf09 == "Rekening bank jauh lebih aman daripada e-wallet"
	replace gf09 = ".d" if gf09 == "Tidak tahu"
	destring gf09, replace
	replace gf09 = .a if gf09 == -99

	la def gf09 1 "E-wallets are much more secure" ///
				2 "E-wallets are somewhat more secure" ///
				3 "Bank accounts are somewhat more secure" ///
				4 "Bank accounts are much more secure", replace

	la val gf09 gf09
	la var gf09 "What is more secure: e-wallets or banks"
	rename gf09 ewallet_bank_secure
	tab ewallet_bank_secure, m

* E-wallet more secure dummy
	gen ewallet_secure = 0 if ewallet_bank_secure != .a
	replace ewallet_secure = 1 if ewallet_bank_secure == 1 | ewallet_bank_secure == 2
	tab ewallet_secure

* Bank more secure dummy
	gen bank_secure = 0 if ewallet_bank_secure != .a
	replace bank_secure = 1 if ewallet_bank_secure == 3 | ewallet_bank_secure == 4
	tab bank_secure

* gf10: Ownership of financial account
	tab gf10, m
	gen ewallet_own = strpos(gf10,"e-wallet") > 0
	gen bank_own = strpos(gf10,"bank") > 0
	gen neither_own = strpos(gf10,"Tidak memiliki keduanya") > 0

	foreach i of varlist *_own {
		replace `i' = .a if gf10 == "-99"
		la val `i' yesno
	}

	la var ewallet_own		 "Own E-Wallet"
	la var bank_own 		 "Own Bank Account"
	la var neither_own 		 "Own Neither E-Wallet nor Bank Account"

	drop gf10

* gf11: Ownership of e-wallet account types
	tab gf11, m
	gen own_jenius = strpos(gf11,"Jenius") > 0
	gen own_ovo = strpos(gf11,"OVO") > 0
	gen own_linkaja = strpos(gf11,"LinkAja") > 0
	gen own_gopay = strpos(gf11,"Gopay") > 0
	gen own_dana = strpos(gf11,"Dana") > 0
	gen own_shopeepay = strpos(gf11,"ShopeePay") > 0
	gen own_others = strpos(gf11,"Lainnya") > 0

	foreach i of varlist own_gopay own_ovo own_linkaja own_dana own_jenius own_shopeepay own_others {
		replace `i' = .a if gf11 == "-99"
		la val `i' yesno
	}

	summ own_*
	la var own_gopay 		"Own E-Wallet: GoPay"
	la var own_ovo 			"Own E-Wallet: OVO"
	la var own_linkaja 		"Own E-Wallet: LinkAja"
	la var own_dana 		"Own E-Wallet: Dana"
	la var own_jenius 		"Own E-Wallet: Jenius"
	la var own_shopeepay 	"Own E-Wallet: ShopeePay"
	la var own_others 		"Own E-Wallet: Others"

	drop gf11

* gf12 & gf18: Purchasing type using e-wallet/bank account
	rename gf12 ewallet
	rename gf18 bank

	foreach v in ewallet bank {
		tab `v', m
		gen `v'_buy_online = strpos(`v',"Ya, untuk membeli barang/jasa online") > 0
		gen `v'_buy_offline = strpos(`v',"Ya, untuk membeli barang/jasa secara tatap muka") > 0
		gen `v'_buy_never = strpos(`v',"Tidak menggunakan") > 0

		foreach i of varlist `v'_* {
			replace `i' = .a if `v' == "-99"
			la val `i' yesno
		}

	la var `v'_buy_online "`v': online purchases"
	la var `v'_buy_offline "`v': in-person purchases"
	la var `v'_buy_never "`v': no purchases"

	gen `v'_buy = 0
	replace `v'_buy = 1 if `v'_buy_online == 1 | `v'_buy_offline == 1
	la val `v'_buy yesno
	la var `v'_buy "`v': made online or offline purchase"
	}

	summ bank_* ewallet_*
	drop ewallet bank

* gf13&gf19: Paid bill services using e-wallet/bank account
	rename gf13 ewallet
	rename gf19 bank

	foreach v in ewallet bank {
		tab `v', m
		gen `v'_bill_mtg = strpos(`v',"Ya, untuk membayar cicilan") > 0
		gen `v'_bill_vphone = strpos(`v',"Ya, untuk membayar tagihan (listrik, air) melalui smartphone") > 0
		gen `v'_bill_vagent = strpos(`v',"Ya, untuk membayar tagihan (listrik, air) melalui agen") > 0
		gen `v'_bill_vbank = strpos(`v',"Ya, untuk membayar tagihan (listrik, air) melalui bank atau ATM") > 0
		gen `v'_bill_never = strpos(`v',"Tidak menggunakan") > 0

		foreach i of varlist `v'_* {
			replace `i' = .a if `v' == "-99"
			la val `i' yesno
		}

		egen `v'_bill_any = rowtotal(`v'_bill_*)
		replace `v'_bill_any = (`v'_bill_any > 0)
		replace `v'_bill_any = .a if `v' == "-99"

		la var `v'_bill_any "`v': pay any bill"
		la var `v'_bill_mtg "`v': payment of mortgage installments"
		la var `v'_bill_vphone "`v': payment of electricity/water bills via mobile phone"
		la var `v'_bill_vagent "`v': payment of electricity/water bills at remote banking agent"
		la var `v'_bill_vbank "`v': payment of electricity/water bills at a bank or ATM"
		la var `v'_bill_never "`v': No bills paid"
	}

	summ bank_* ewallet_*
	drop ewallet bank

	la val ewallet_bill_any yesno
	la val bank_bill_any yesno

* gf14&gf24: Utilization of bank feature in e-wallet/bank account
	rename gf14 ewallet
	rename gf20 bank

	foreach v in ewallet bank {
		tab `v', m
		gen `v'_ft_save = strpos(`v',"Tabungan/Investasi") > 0
		gen `v'_ft_goods = strpos(`v',"Menerima pembayaran dari orang lain sebagai hasil penjualan") > 0
		gen `v'_ft_send = strpos(`v',"Memberi uang kepada orang lain") > 0
		gen `v'_ft_get = strpos(`v',"Menerima kiriman uang dari orang lain") > 0
		gen `v'_ft_wage = strpos(`v',"Menerima gaji dan/atau tunjangan") > 0
		gen `v'_ft_deposit = strpos(`v',"Menyetor/menyimpan uang tunai") > 0
		gen `v'_ft_wdraw = strpos(`v',"Menarik uang tunai") > 0
		gen `v'_ft_none = strpos(`v',"Tidak menggunakan") > 0

		foreach i of varlist `v'_* {
			replace `i' = .a if `v' == "-99"
			la val `i' yesno
		}

		egen `v'_ft_any = rowtotal(`v'_ft_*)
		replace `v'_ft_any = (`v'_ft_any > 0)
		replace `v'_ft_any = .a if `v' == "-99"
		
		la var `v'_ft_any "`v': use any feature"
		la var `v'_ft_save "`v': use account for Savings/Investment"
		la var `v'_ft_goods "`v': use account for receiving payments for sales of Goods and Services"
		la var `v'_ft_send "`v': use account for Sending money to others"
		la var `v'_ft_get "`v': use account for receiving money from others"
		la var `v'_ft_wage "`v': use account for receiving wage payments"
		la var `v'_ft_deposit "`v': use account for Making deposits into the account"
		la var `v'_ft_wdraw "`v': use account for Withdrawing cash from the account"
		la var `v'_ft_none "`v': use account for No, I have not used any features"
	}

	summ bank_* ewallet_*
	drop ewallet bank

	la val ewallet_ft_any yesno
	la val bank_ft_any yesno

* gf15&gf21: Total transaction using e-wallet/bank account
	tab gf21 , m

	la def trx_freq 1 "Never" ///
					2 "Once" ///
					3 "A few times a month" ///
					4 "A few times a week" ///
					5 "Almost every day" ///
					6 "Every day" ///
					7 "Multiple times every day", replace

	foreach var in gf15 gf21 {
		replace `var' = "1" if `var' == "Tidak pernah"
		replace `var' = "2" if `var' == "Satu kali"
		replace `var' = "2" if `var' == "Sekali satu kali"
		replace `var' = "3" if `var' == "Beberapa kali dalam sebulan"
		replace `var' = "4" if `var' == "Beberapa kali dalam seminggu"
		replace `var' = "5" if `var' == "Hampir setiap hari"
		replace `var' = "6" if `var' == "Setiap hari"
		replace `var' = "7" if `var' == "Beberapa kali setiap hari"
		destring `var', replace
		replace `var' = .a if `var' == -99
		la val `var' trx_freq
	}

	rename gf15 ewallet_trans
	la var ewallet_trans "How many e-wallet transactions did you make in the past month?"

	gen ewallet_one_trans = ewallet_trans > 1
	replace ewallet_one_trans = .a if ewallet_trans == .a
	la val ewallet_one_trans yesno
	la var ewallet_one_trans "Made at least one e-wallet transaction in the last month"

	gen ewallet_multi_trans = ewallet_trans > 2
	replace ewallet_multi_trans = .a if ewallet_trans == .a	
	la val ewallet_multi_trans yesno
	la var ewallet_multi_trans "Made multiple e-wallet transactions in the last month"

	rename gf21 bank_trans
	la var bank_trans "How many bank/debit card transactions did you make in the past month?"

	gen bank_one_trans = bank_trans > 1
	replace bank_one_trans = .a if bank_trans == .a
	la val bank_one_trans yesno
	la var bank_one_trans "Made at least one bank transaction in the last month"

	gen bank_multi_trans = bank_trans > 2
	replace bank_multi_trans = .a if bank_trans == .a	
	la val bank_multi_trans yesno
	la var bank_multi_trans "Made multiple bank transactions in the last month"

* gf16: How do you fill your e-wallet
	tab gf16, m
	gen ewallet_top_bank  = strpos(gf16, "Menggunakan akun bank") > 0
	gen ewallet_top_agent = strpos(gf16, "Melalui agen") > 0
	gen ewallet_top_app   = strpos(gf16, "Melalui aplikasi") > 0
	gen ewallet_top_never = strpos(gf16, "Saya belum pernah") > 0

	foreach var of varlist ewallet_top_* {
		replace `var' = .a if gf16 == "-99"
		la val `var' yesno
	}

	summ ewallet_top_*

	la var ewallet_top_bank 	"E-Wallet top-up method: Bank"
	la var ewallet_top_agent 	"E-Wallet top-up method: Agent"
	la var ewallet_top_app 		"E-Wallet top-up method: App"
	la var ewallet_top_never 	"E-Wallet top-up method: Never"

	drop gf16

* gf17: Ownership of bank account types
	tab gf17, m

	gen ewallet_himbara  = strpos(gf17,"Himbara") > 0
	gen ewallet_regional = strpos(gf17,"Pembangunan") > 0
	gen ewallet_private  = strpos(gf17,"Swasta") > 0

	foreach i of varlist ewallet_himbara ewallet_regional ewallet_private {
		replace `i' = .a if gf17 == "-99"
		la val `i' yesno
	}

	la var ewallet_himbara  	"Own bank account in state-owned bank"
	la var ewallet_regional 	"Own bank account in regional bank"
	la var ewallet_private  	"Own bank account in State-owned bank"
	drop gf17

* gf22: Online purchases (other than for Prakerja) in the last 3 months
	tab gf22, m

	gen purch_tokopedia = strpos(gf22,"Tokopedia") > 0
	gen purch_bukalapak = strpos(gf22,"Bukalapak") > 0
	gen purch_pintaria = strpos(gf22,"Pintaria") > 0
	gen purch_pijar = strpos(gf22,"Pijar Mahir") > 0
	gen purch_maubelajar = strpos(gf22,"Mau Belajar Apa") > 0
	gen purch_sekolahmu = strpos(gf22,"Sekolahmu") > 0
	gen purch_shopee = strpos(gf22,"Shopee") > 0
	gen purch_never = strpos(gf22,"Tidak melakukan transaksi pembelian di platform online") > 0
	gen purch_others = strpos(gf22,"Lainnya") > 0

	foreach var of varlist purch_* {
		replace `var' = .a if gf22 == "-99"
		la val `var' yesno
	}

	summ purch_*

	la var purch_tokopedia  	"Online purchases in last 3 months: Tokopedia"
	la var purch_bukalapak  	"Online purchases in last 3 months: Bukalapak"
	la var purch_pintaria   	"Online purchases in last 3 months: Pintaria"
	la var purch_pijar      	"Online purchases in last 3 months: Pijar Mahir"
	la var purch_maubelajar 	"Online purchases in last 3 months: Mau Belajar Apa"
	la var purch_sekolahmu  	"Online purchases in last 3 months: Sekolahmu"
	la var purch_shopee     	"Online purchases in last 3 months: Shopee"
	la var purch_never      	"Online purchases in last 3 months: No transaction"
	la var purch_others     	"Online purchases in last 3 months: Others"

	drop gf22

	local eb_list own ///
				  buy ///
				  buy_online ///
				  buy_offline ///
				  bill_any ///
				  bill_mtg ///
				  bill_vphone ///
				  bill_vagent ///
				  bill_vbank ///
				  one_trans ///
				  multi_trans ///
				  ft_any ///
				  ft_save ///
				  ft_goods ///
				  ft_send ///
				  ft_get ///
				  ft_wage ///
				  ft_deposit ///
				  ft_wdraw


	foreach i in `eb_list' {
		gen eb_`i' = 0
		replace eb_`i' = 1 if ewallet_`i' == 1 | bank_`i' == 1
		replace eb_`i' = .a if ewallet_`i' == .a | bank_`i' == .a
		la val eb_`i' yesno
	}


/*----------------------------------------------------*/
                  /* Section: ps */
/*----------------------------------------------------*/

*ps01: depression
	tab ps01, m
	la def depression 1 "Never" 2 "Several days" 3 "More than half the days" 4 "Nearly every day", replace

	foreach var of varlist ps01 ps02 {
		assert missing(`var') if !missing(`var'_a)
		replace `var' = `var'_a if !missing(`var'_a)
		replace `var' = "1" if `var' == "Tidak pernah"
		replace `var' = "2" if `var' == "Beberapa hari"
		replace `var' = "3" if `var' == "Lebih dari setengah waktu, dalam 2 minggu terakhir"
		replace `var' = "4" if `var' == "Hampir setiap hari"
		destring `var', replace
		replace `var' = .a if `var' == -99
		la val `var' depression
		tab `var', m
	}

	rename ps01 depressed
	la var depressed "Over the last 2 weeks, how often have you felt down, depressed, or hopeless?"

	rename ps02 disinterested
	la var disinterested "In the last 2 weeks, how often did you feel disinterested/uninterested in activities that you did in your daily life?"

*ps03-ps04: achieving goals, deal with unexpected events, solve problems
	la def agree 1 "Disagree" 2 "Somewhat disagree" 3 "Somewhat agree" 4 "Agree", replace
	foreach var of varlist ps03 ps04 ps05 {
		tab `var', m
		assert missing(`var') if !missing(`var'_a)
		replace `var' = `var'_a if !missing(`var'_a)
		replace `var' = "1" if `var' == "Tidak setuju"
		replace `var' = "2" if `var' == "Agak setuju"
		replace `var' = "3" if `var' == "Hampir setuju"
		replace `var' = "4" if `var' == "Sangat setuju"
		destring `var', replace
		replace `var' = .a if `var' == -99
		la val `var' agree
		tab `var', m
	}

	rename ps03 accomplish_goals
	la var accomplish_goals "It is easy for me to stick to my aims and accomplish my goals. "

	rename ps04 deal_uncertainty
	la var deal_uncertainty "I am confident that I could deal efficiently with unexpected events. "

	rename ps05 solve_problems
	la var solve_problems "I can solve most problems if I invest the necessary effort."

* ps05: confidence in starting business
	tab ps06, m
	assert missing(ps06) if !missing(ps06_a)
	replace ps06 = ps06_a if !missing(ps06_a)
	replace ps06 = "1" if ps06 == "Sangat tidak percaya diri"
	replace ps06 = "2" if ps06 == "Tidak percaya diri"
	replace ps06 = "3" if ps06 == "Percaya diri"
	replace ps06 = "4" if ps06 == "Sangat percaya diri"
	destring ps06, replace
	replace ps06 = .a if ps06 == -99
	la def confidence 1 "very not confident" 2 "Not confident" 3 "Confident" 4 "very confident", replace
	la val ps06 confidence
	rename ps06 business_ability
	la var business_ability  "How confident do you feel in your ability to start a business?"

	drop ps0*_a

* Normalize
	foreach var in depressed ///
				   disinterested ///
				   accomplish_goals ///
				   deal_uncertainty ///
				   solve_problems ///
				   business_ability {
		gen `var'_n = (`var'/4)
	}

* Aggregate
	gen depress_disint = depressed + disinterested
	gen depress_disint_n =depress_disint/8
	gen efficacy = accomplish_goals + deal_uncertainty + solve_problems
	gen efficacy_n = efficacy/12
/*----------------------------------------------------*/
 * Section 17: KF - Format KP financial questions
/*----------------------------------------------------*/

* kf01: Ownership of financial account
	tab kf01, m
	replace kf01 = "1" if strpos(kf01,"e-wallet") > 0
	replace kf01 = "2" if strpos(kf01,"bank") > 0
	replace kf01 = "3" if strpos(kf01,"Memiliki keduanya") > 0
	replace kf01 = "4" if strpos(kf01,"Tidak memiliki keduanya") > 0
	destring kf01, replace
	replace kf01 = .a if kf01 == -99

	la def fin_acct_before_kp 1 "E-wallet" ///
							  2 "Bank account" ///
							  3 "Had both" ///
							  4 "Had neither", replace

	la val kf01 fin_acct_before_kp
	la var kf01 "Before applying to KP, did you have a bank account or e-wallet?"
	tab kf01, m

* kf02-kf03: Distance from home to ATM/cash withdrawal point

	forval i = 2/3 {
		* replace with corrected answer
		replace kf0`i'_1_text = kf0`i'_1_1_text if !missing(kf0`i'_1_1_text)

		* don't know dummy
		gen kf0`i'_dont_know = 1 if strpos(kf0`i', "Tidak tahu") > 0 & !missing(kf0`i')

		* first remove non digits, then destring
		* Note: both kf0`i' and kf0`i'_1_text can have values of -99. If kf0`i' has value of -99 then that means question was seen but skipped, if kf0`i'_1_text has value of -99, that means the respondent answered don't know
		rename kf0`i'_1_text kf0`i'_clean
		replace kf0`i'_clean = .d if kf0`i'_dont_know == 1
		drop kf0`i' kf0`i'_1_1_text
		rename kf0`i'_clean kf0`i'
	}

	summ kf02 kf03
	la var kf02 "Distance from your house to the nearest bank or ATM (in km)"
	la var kf03 "Distance from your house to the nearest e-wallet withdrawal point (in km)"

* kf04: KP payment method
	tab kf04, m
	replace kf04 = "1" if strpos(kf04,"BNI") > 0
	replace kf04 = "2" if strpos(kf04,"OVO") > 0
	replace kf04 = "3" if strpos(kf04,"Link") > 0
	replace kf04 = "4" if strpos(kf04,"Gopay") > 0
	replace kf04 = "5" if strpos(kf04,"Dana") > 0
	replace kf04 = "95" if strpos(kf04,"Lainnya") > 0
	destring kf04, replace
	replace kf04 = .a if kf04 == -99

	la def kf04 1 "BNI" 2 "OVO" 3 "Linkaja" 4 "Gopay" 5 "Dana" 95 "Other", replace
	la val kf04 kf04
	la var kf04 "Method of payment for transactions in Kartu Prakerja?"
	tab kf04, m

* kf05: reason for choosing payment method
	tab kf05, m
	replace kf05 = "1" if strpos(kf05,"Sudah memiliki akun") > 0
	replace kf05 = "2" if strpos(kf05,"Kemudahan dalam mencairkan saldo") > 0
	replace kf05 = "3" if strpos(kf05,"Kemudahan dalam membuat/membuka") > 0
	replace kf05 = "4" if strpos(kf05,"Kemudahan dalam menggunakan akun") > 0
	replace kf05 = "5" if strpos(kf05,"Pengaruh dari teman, keluarga") > 0
	replace kf05 = "95" if strpos(kf05,"Lainnya") > 0
	destring kf05, replace
	replace kf05 = .a if kf05 == -99

	la def kf05 1 "Already had existing account" ///
				2 "Ease of physical access withdrawing cash" ///
				3 "Ease of setting up a new account" ///
				4 "Ease of using account for other purposes" ///
				5 "Influence of peers, family, program managers, or others" ///
				95 "Others", replace
	la val kf05 kf05
	la var kf05 "Main reason for choosing this payment method?"
	tab kf05, m

* kf06: Satisfaction rate when using the prefered Prakerja transaction mode
	tab kf06, m
	replace kf06 = "1" if strpos(kf06,"Sangat tidak memuaskan") > 0
	replace kf06 = "2" if strpos(kf06,"Tidak memuaskan") > 0
	replace kf06 = "3" if strpos(kf06,"Lumayan memuaskan") > 0
	replace kf06 = "4" if strpos(kf06,"Sangat memuaskan") > 0
	destring kf06, replace
	replace kf06 = .a if kf06 == -99

	la var kf06 "Overall, how would you rate your experience with the payment method?"
	la def kf06 1 "Not at all satisfied" ///
				2 "Not satisfied" ///
				3 "Somewhat satisfied" ///
				4 "Very satisfied", replace
	la val kf06 kf06
	tab kf06, m

* kf07-kf08: Prefered/not prefered feature in e-wallet
	tab kf07, m
	replace kf07 = "1" if strpos(kf07,"Tampilan") > 0
	replace kf07 = "2" if strpos(kf07,"keamanan") > 0
	replace kf07 = "3" if strpos(kf07,"Banyak tipe transaksi") > 0
	replace kf07 = "4" if strpos(kf07,"Biaya adminsitrasi") > 0
* accommodate misspelling in survey
	replace kf07 = "4" if strpos(kf07,"Biaya administrasi") > 0
	replace kf07 = "5" if strpos(kf07,"Dapat digunakan di smartphone") > 0
	replace kf07 = "6" if strpos(kf07,"voucher, cashback") > 0
	replace kf07 = "95" if strpos(kf07,"Lainnya") > 0
	destring kf07, replace
	replace kf07 = .a if kf07 == -99

	la def prefer 1 "Interface" ///
				  2 "Safety" ///
				  3 "Variety of transaction" ///
				  4 "Admin/transfer fee cheaper" ///
				  5 "Can use smartphones" ///
				  6 "Vouchers, cashbacks" ///
				  95 "Other", replace
	la val kf07 prefer
	la var kf07 "What is the feature that you liked most about the e-wallet account?"
	tab kf07, m

	tab kf08, m
	replace kf08 = "1" if strpos(kf08,"Tampilan") > 0
	replace kf08 = "2" if strpos(kf08,"keamanan") > 0
	replace kf08 = "3" if strpos(kf08,"e-wallet rumit") > 0
	replace kf08 = "4" if strpos(kf08,"dapat digunakan di smartphone") > 0
	replace kf08 = "5" if strpos(kf08,"tidak berbunga") > 0
	replace kf08 = "95" if strpos(kf08,"Lainnya") > 0
	destring kf08, replace
	replace kf08 = .a if kf08 == -99

	la def nprefer 1 "Interface" ///
				   2 "Safety" ///
				   3 "Too complicated" ///
				   4 "Can only use smartphones" ///
				   5 "Not interested" ///
				   95 "Others", replace
	la val kf08 nprefer
	la var kf08 "What is the feature that you liked least about the e-wallet account?"
	tab kf08, m

* kf09: converted your e-wallet balance into cash
	tab kf09, m
	replace kf09 = "1" if kf09 == "Pernah"
	replace kf09 = "0" if kf09 == "Belum pernah"
	destring kf09, replace
	replace kf09 = .a if kf09 == -99
	la val kf09 yesno
	la var kf09 "Have you ever converted your e-wallet balance into cash?"
	tab kf09, m

* kf10: last time you converted your e-wallet balance into cash
	gen kf10 = mdy(kf10_1, 15, kf10_2)
	replace kf10 = .a if kf10_1 == .a | kf10_2 == .a
	format kf10 %td
	la var kf10 "When was the last time you converted your e-wallet balance into cash?"
	drop kf10_1 kf10_2
	summ kf10, format

* kf11: amount you withdrew the most recent time you withdrew cash
	gen kf11_dont_know = kf11 == "Tidak tahu" if !missing(kf11)

	rename kf11_5_text kf11_clean
	replace kf11_clean = kf11_1_5_text if !missing(kf11_1_5_text)
	replace kf11_clean = .d if kf11_dont_know == 1
	drop kf11 kf11_1_5_text
	rename kf11_clean kf11

	la var kf11 "What was the amount you withdrew at that time?"
	summ kf11

* kf12: withdraw the entire balance, or did you leave some money remaining in the account
	tab kf12, m
	replace kf12 = "1" if  strpos(kf12,"seluruh isi saldo e-wallet") > 0
	replace kf12 = "3" if  strpos(kf12,"saldo yang tersisa") > 0
	replace kf12 = "95" if  strpos(kf12,"Tidak tahu") > 0
	destring kf12, replace
	replace kf12 = .a if kf12 == -99

	la def kf12 1 "Withdrew entire balance" ///
				3 "Left some money in the account" ///
				95 "I don't know", replace
	la val kf12 kf12
	la var kf12 "Did you withdraw the entire balance at that time, or did you leave some money?"
	tab kf12, m

* kf13: Where did you withdraw the cash"
	tab kf13, m
	replace kf13 = "1" if  strpos(kf13,"ATM") > 0
	replace kf13 = "2" if  strpos(kf13,"Bank") > 0
	replace kf13 = "3" if  strpos(kf13,"Agen") > 0
	replace kf13 = "4" if  strpos(kf13,"Toko Serba Ada") > 0
	replace kf13 = "9" if  strpos(kf13,"Tidak tahu") > 0
	replace kf13 = "95" if  strpos(kf13,"Lainnya") > 0
	destring kf13, replace
	replace kf13 = .a if kf13 == -99

	la def kf13 1 "ATM" ///
				2 "Bank branch" ///
				3 "Remote banking agent" ///
				4 "Convenience store" ///
				95 "Other", replace
	replace kf13 = .d if kf13 == 9
	la val kf13 kf13
	la var kf13 "Where did you withdraw the cash at that time?"
	tab kf13, m

* kf14: admin fee for cash withdrawal
	gen kf14_dont_know = kf14 == "Tidak tahu" if !missing(kf14)

	rename kf14_1_text kf14_clean
	replace kf14_clean = kf14_1_1_text if !missing(kf14_1_1_text)
	replace kf14_clean = .d if kf14_dont_know == 1
	drop kf14 kf14_1_1_text
	rename kf14_clean kf14

	la var kf14 "What was the fee that was charged to withdraw the cash at that time?"
	summ kf14


/************************************************************
	Compensation Payments (section PM)
**************************************************************/

* TRADE-OFF BETWEEN E-WALLET AND PHONE CREDIT VARIABLES

* There are 9 combinations of values * 2 methods of showing showing each combination (phone credit first or ewallet first), so total 18 variables

	foreach var of varlist pm01_* {
		replace `var' = "1" if strpos(`var', "E-wallet") > 0
		replace `var' = "1" if strpos(`var', "e-wallet") > 0
		replace `var' = "0" if strpos(`var', "pulsa") > 0
		destring `var', replace
		replace `var' = .a if `var' == -99
		tab `var', m
	}

	forval i = 1/9 {
		replace pm01_`i'_p = pm01_`i'_e if !missing(pm01_`i'_e)
		drop pm01_`i'_e
	}

* Combine with old PM01-7 from before July 2 edits
	forval i = 1/7 {
		replace pm01_`i'_p = pm01_`i' if !missing(pm01_`i')
		drop pm01_`i'
	}

	la def pm01_1_p 1 "50K e-wallet" 0 "50K phone credit", modify
	la def pm01_2_p 0 "50K phone credit" 1 "55K e-wallet", modify
	la def pm01_3_p 0 "50K phone credit" 1 "60K e-wallet", modify
	la def pm01_4_p 0 "50K phone credit" 1 "65K e-wallet", modify
	la def pm01_5_p 1 "50K e-wallet" 0 "55K phone credit", modify
	la def pm01_6_p 1 "50K e-wallet" 0 "60K phone credit", modify
	la def pm01_7_p 1 "50K e-wallet" 0 "65K phone credit", modify
	la def pm01_8_p 1 "50K e-wallet" 0 "70K phone credit", modify
	la def pm01_9_p 1 "50K e-wallet" 0 "75K phone credit", modify

* Labels
	la var pm01_1_p "Which would you prefer: 50K phone credit vs. 50K e-wallet?"
	la var pm01_2_p "Which would you prefer: 50K phone credit vs. 55K e-wallet?"
	la var pm01_3_p "Which would you prefer: 50K phone credit vs. 60K e-wallet?"
	la var pm01_4_p "Which would you prefer: 50K phone credit vs. 65K e-wallet?"
	la var pm01_5_p "Which would you prefer: 55K phone credit vs. 50K e-wallet?"
	la var pm01_6_p "Which would you prefer: 60K phone credit vs. 50K e-wallet?"
	la var pm01_7_p "Which would you prefer: 65K phone credit vs. 50K e-wallet?"
	la var pm01_8_p "Which would you prefer: 70k phone credit vs. 50k e-wallet?"
	la var pm01_9_p "Which would you prefer: 75k phone credit vs. 50k e-wallet?"

* rename for ordering from highest phone credit -> lowest phone credit, and lowest e-wallet credit -> highest e-wallet credit
	rename pm01_9_p ewalletvsphone_1 // 50k E-Wallet vs. 75k Phone Credit
	rename pm01_8_p ewalletvsphone_2 // 50k E-Wallet vs. 70k Phone Credit
	rename pm01_7_p ewalletvsphone_3 // 50k E-Wallet vs. 65k Phone Credit
	rename pm01_6_p ewalletvsphone_4 // 50k E-Wallet vs. 60k Phone Creditno
	rename pm01_5_p ewalletvsphone_5 // 50k E-Wallet vs. 55k Phone Credit
	rename pm01_1_p ewalletvsphone_6 // 50k E-Wallet vs. 50k Phone Credit
	rename pm01_2_p ewalletvsphone_7 // 55k E-Wallet vs. 50k Phone Credit
	rename pm01_3_p ewalletvsphone_8 // 60k E-Wallet vs. 50k Phone Credit
	rename pm01_4_p ewalletvsphone_9 // 65k E-Wallet vs. 50k Phone Credit

	tab ewalletvsphone_5

	egen choose_ewallet = rowtotal(ewalletvsphone_*)
	label var choose_ewallet "Choose E-Wallet"


/*----------------------------------------------------*/
                  /* Section: pm */
/*----------------------------------------------------*/

* pm04: whose phone number
	tab pm04, m
	replace pm04 = "1" if pm04 == "Saya/milik pribadi"
	replace pm04 = "2" if pm04 == "Pasangan/suami/istri"
	replace pm04 = "3" if pm04 == "Keluarga/anggota rumah tangga"
	replace pm04 = "4" if pm04 == "Teman"
	replace pm04 = "5" if pm04 == "Lainnya"
	destring pm04, replace
	replace pm04 = .a if pm04 == -99
	la def pm04 1 "Mine" 2 "Spouse's" 3 "Family's/HH Member's" 4 "Friend's" 5 "Other", replace
	la val pm04 pm04
	la var pm04 "Whose phone number is this?"
	tab pm04, m

* pm06: ewallet type
	tab pm06, m
	replace pm06 = "1" if  strpos(pm06,"Go") > 0
	replace pm06 = "2" if  strpos(pm06,"OVO") > 0
	replace pm06 = "3" if  strpos(pm06,"LinkAja") > 0
	replace pm06 = "4" if  strpos(pm06,"Dana") > 0
	destring pm06, replace
	replace pm06 = .a if pm06 == -99

	la def pm06 1 "Gopay" 2 "OVO" 3 "LinkAja" 4 "Dana", replace
	la val pm06 pm06
	la var pm06 "Please select the e-wallet of your choice to receive compensation"
	tab pm06, m

* pm07: whose phone number and e-wallet
	tab pm07, m
	replace pm07 = "1" if pm07 == "Saya/milik pribadi"
	replace pm07 = "2" if pm07 == "Pasangan/suami/istri"
	replace pm07 = "3" if pm07 == "Keluarga/anggota rumah tangga"
	replace pm07 = "4" if pm07 == "Teman"
	replace pm07 = "5" if pm07 == "Lainnya"
	destring pm07, replace
	replace pm07 = .a if pm07 == -99
	la val pm07 pm04
	la var pm07 "Whose phone number and e-wallet is this?"
	tab pm07, m
	
* Incentive number
gen incentive_number = pm03
replace incentive_number = pm05 if pm05 != ""

/*----------------------------------------------------*/
 * Section 19: Clean duplicates and scam responses
/*----------------------------------------------------*/

*If duplicates by anon_id4, keep those which pass all criterion and then those which are completed, and then sort by startdate
	gen include = (consented == 1 & verified ==1 & completed ==1)

	bysort anon_id4 (include completed consented verified startdate): keep if _n == _N

	drop include

/*----------------------------------------------------*/
             /* Section: Output Data */
/*----------------------------------------------------*/

* Confirm anon_id is ID
	isid anon_id4

* merge with roster
	rename batch year_batch
	la var year_batch "Year of Associated Batch (for display)"
	
	merge 1:1 anon_id4 using "$KP_deid_survey/2021/Raw/roster_master_final_deid.dta", keep(1 2 3 4 5) keepusing(sent_survey* gender sample_round cohort) gen(merge_roster) update 
	replace sent_survey = 0 if mi(cohort)

* export version un-merged with PMO data
	preserve 
		compress
		drop if merge_roster==2
		datasignature 
	  if "`r(datasignature)'" == "64933:1481(20507):2419290385:1741954360" {
	    save "$KP_deid_survey/2021/Clean/071323_survey_21_deid_clean.dta", replace
	      }
	  else {
	    di as err "Careful, your machine produces a different dataset"
	    stop
			}
		
	restore 
	
* merge with admin data
	merge 1:m anon_id4 using "$KP_deid_admin/Clean/pmo_b1-22_clean_long_deid.dta", keep(1 3 4 5) nogen keepusing(gender batch strata stratum_win_prob urban yrs_school multi_apply_hh win_in_batch ever_win_17 ever_win_22 ever_win_39 education age final_test_score java date_incentive) update
	keep if sample_round == batch
	tab sample_round batch

* final check of the missing data values
	mdesc

* Save full sample
	compress
	datasignature 
	  if "`r(datasignature)'" == "275395:1496(58427):1975129341:775400709" {
	    save "$KP_deid_survey/2021/Clean/071323_survey_21_deid_clean_merged.dta", replace
	      }
	  else {
	    di as err "Careful, your machine produces a different dataset"
	    stop
			}
	

// DONE
